//
//  JSViewController.m
//  UITest
//
//  Created by wp w on 12-6-28.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "JSViewController.h"
#import "DLog.h"

@interface JSViewController ()

@end

@implementation JSViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    webview = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    webview.backgroundColor = [UIColor clearColor];  
    webview.scalesPageToFit =YES;
    webview.delegate =self;
    NSURL *url =[[NSURL alloc] initWithString:@"http://www.google.com.hk/m?gl=CN&hl=zh_CN&source=ihp"];
    
    NSURLRequest *request =  [[NSURLRequest alloc] initWithURL:url];
    [webview loadRequest:request];  
    [self.view addSubview:webview];
    
    
	// Create the segmented control. Choose one of the three styles
	NSArray *buttonNames = [NSArray arrayWithObjects:@"One", @"Two", @"Three", @"Four", @"Five", @"Six", nil];
	UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:buttonNames];
	segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; 
	[segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
	segmentedControl.momentary = YES;   //选中状态，是否时鼠标离开后消失
	
    
	// Add it to the navigation bar
	self.navigationItem.titleView = segmentedControl;
	[segmentedControl release];
}

-(void) segmentAction: (UISegmentedControl *) sender
{
    NSInteger index = sender.selectedSegmentIndex;
    switch (index) {
        case 0:     //直接操作js代码
        {
            //获取当前页面的url
            NSString *currentURL = [webview stringByEvaluatingJavaScriptFromString:@"document.location.href"];
            LogStr(currentURL);
            
            //获取页面title
            NSString *title = [webview stringByEvaluatingJavaScriptFromString:@"document.title"]; 
            LogStr(title);
            
            //修改界面元素的值
            NSString *js_result = [webview stringByEvaluatingJavaScriptFromString:@"document.getElementsByName('q')[0].value='朱祁林';"];
            LogStr(js_result);
            
            //表单提交
            NSString *js_result2 = [webview stringByEvaluatingJavaScriptFromString:@"document.forms[0].submit(); "];
            LogStr(js_result2);
        } 
            break;
            
        case 1: //组装成js function，然后调用
        {
            [webview stringByEvaluatingJavaScriptFromString:@"var script = document.createElement('script');"  
                 "script.type = 'text/javascript';"  
                 "script.text = \"function myFunction() { "  
                 "var field = document.getElementsByName('q')[0];"  
                 "field.value='朱祁林';"  
                 "document.forms[0].submit();"  
                 "}\";"  
                 "document.getElementsByTagName('head')[0].appendChild(script);"];   
            
            [webview stringByEvaluatingJavaScriptFromString:@"myFunction();"]; 
        }
            break;
            
        default:
            break;
    }
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

-(void)dealloc
{
    [webview release];
    [super dealloc];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark - UIWebViewDelegate
- (void)webViewDidFinishLoad:(UIWebView *)webView {  
    //获取当前页面的url
    NSString *currentURL = [webView stringByEvaluatingJavaScriptFromString:@"document.location.href"];
    LogStr(currentURL);
    
    //获取页面title
    NSString *title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"]; 
    LogStr(title);
}

@end
